let config = require('./config');
let Wechat = require('./wechat/wechat');
let wechatApi =  new Wechat(config.wxConfig)
exports.reply = function *(next){
    let message = this.weixin;
    if(message.MsgType === 'event') {  //用户事件操作
        if(message.Event === 'subscribe') {
            console.log('已经关注')
            if(message.EventKey) {
                console.log('扫二维码进来：' + message.EventKey + ' ' + message.ticket);
            }
            this.body = '欢迎进入黄某的测试号';
        }else if(message.Event === 'unsubscribe') {
            console.log('无情取消关注!');
            this.body = '';
        }else if(message.Event === 'LOCATION') {
            this.body = '您上报的位置是：' + message.Latitude + '/' + message.Longitude + '-' + message.Precision
        }else if(message.Event === 'CLICK')  {
            this.body = '您点击了菜单：' + message.EventKey
        }else if(message.Event === 'SCAN') {
            console.log('关注后扫描二维码:'+ message.EventKey + ' ' + message.Ticket);
            this.body = '你在我的公众号里面使用扫描二维码了哦'
        }else if(message.Event === 'VIEW') {
            this.body = '你点击了菜单中的链接：' +  message.EventKey
        }
    }else if(message.MsgType === 'text') {
        let content = message.Content;
        let reply ='sorry,我不明白' + content + '是什么意思！'
        if(content === '1') {
            reply = '一个孩子坐飞机'
        }else if(content === '2') {
            reply = '两个孩子是傻逼'
        }else if(content === '文章'){
            reply = [
                {
                    title: '成功人士速成攻略',
                    description: '描述了如何能达到像黄某这么一个高度',
                    picUrl: 'http://mpic.tiankong.com/f95/3dc/f953dcb9815ba433a3c16e8b9d8874b7/640.jpg@360h',
                    url: 'http://www.baidu.com'
                }
            ]
        }else if(content === '图片') {
            let data = yield wechatApi.uploadMaterial('image',__dirname + '/static/image/1.jpg');
            reply = {
                type: 'image',
                mediaId: data.media_id
            }
        }else if(content === '视频') {
            let data = yield wechatApi.uploadMaterial('video',__dirname + '/static/video/1.mp4');
            reply = {
                type : 'video',
                mediaId: data.media_id,
                title: '家里的工作环境',
                description: '展示下家里的日常开发环境'
            }
        }else if(content === '音乐') {
            let data = yield wechatApi.uploadMaterial('video',__dirname + '/static/video/1.mp4');
            reply = {
                type : 'music',
                title: '动听的音乐',
                description: '轻松一刻',
                musicUrl: 'http://www.kuwo.cn/down/single/6170631',
                thumbMediaId: data.media_id
            }
        }

        this.body = reply;
    }
    yield next;
}